<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app;

//require_once "keygen.php";
//require_once "generator_dk.php";
use think\App;
use think\exception\HttpResponseException;
use think\Response;

abstract class BaseController
{
    protected $request;
    protected $app;
    protected $batchValidate = false;
    protected $middleware = [];
    public $_app;
    public $_controller;
    public $_action;
    public $_method = "GET";
    public $_param = [];
    public $_input = [];
    public $_header = [];
    public $_host;
    public $_ip;
    public $_is_weiqin = false;
    // @function __construct is protected ioncube.dynamickey encoding key.
    public function __construct(App $app)
    {

        $this->request = request();
        $this->_param = $this->request->param();
//        var_dump($this->_param);
        $this->_token = $this->request->header("token");
        $this->_autograph = $this->request->header("autograph");
        $this->_method = $this->request->method();
        //获取body参数
        $this->_input      = json_decode( $this->request->getInput(), true );
//        print_r($this->request->param());
        $this->_host = $this->request->host();
        $this->_ip = $this->request->ip();
        $this->_is_weiqin = longbingIsWeiqin();
        $this->_header = $this->request->header();

        $this->app = app();
        $this->_app = app();
        $this->_controller = request()->controller();
        $this->_action = request()->action();
//        $this->_uniacid = intval($this->request->param("i"));
    }
    public function shareChangeDatasssss($action)
    {
        $arr = ["clearCache", "noLookCount", "getW7TmpV2", "getSaasAuth", "isWe7", "getConfig", "login", "adminNodeInfo"];
        if(!empty($action) && in_array($action, $arr)) {
            return false;
        }
        return true;
    }
    // @function isAuth is protected ioncube.dynamickey encoding key.
    public function isAuth()
    {
    }
    protected function errorMsg($msg = "", $code = 400)
    {
        $msg = \think\facade\Lang::get($msg);
        $this->results($msg, $code);
    }


    /**
     * 返回封装后的 API 数据到客户端
     * @access protected
     * @param mixed  $msg    提示信息
     * @param mixed  $data   要返回的数据
     * @param int    $code   错误码，默认为0
     * @param string $type   输出类型，支持json/xml/jsonp
     * @param array  $header 发送的 Header 信息
     * @return void
     * @throws HttpResponseException
     */
    protected function results($msg, $code, array $header = [])
    {
        $result = [
            'error' => $msg,
            'code'  => $code,
        ];
        $response = Response::create($result, 'json', 200)->header($header);
        throw new HttpResponseException($response);
    }


    protected function initialize()
    {
    }
    public function success($data, $code = 200)
    {
        $result["data"] = $data;
        $result["code"] = $code;
        $result["sign"] = NULL;
        $result["return_code"] = "SUCCESS";
        $result["return_msg"] = "OK";
        if(!empty($this->_token)) {
            $result["sign"] = createSimpleSign($this->_token, is_string($data) ? $data : json_encode($data));
        }
        return $this->response($result, "json", $code);
    }
    public function error($msg, $code = 400)
    {
        $result["error"] = \think\facade\Lang::get($msg);
        $result["code"] = $code;
        return $this->response($result, "json", 200);
    }
    protected function response($data, $type = "json", $code = 200)
    {
        return \think\Response::create($data, $type)->code($code);
    }
    protected function validate(array $data, $validate, array $message = [], $batch = false)
    {
        if(is_array($validate)) {
            $v = new \think\Validate();
            $v->rule($validate);
        } else {
            if(strpos($validate, ".")) {
                list($validate, $scene) = explode(".", $validate);
            }
            $class = false !== strpos($validate, "\\") ? $validate : $this->app->parseClass("validate", $validate);
            $v = new $class();
            if(!empty($scene)) {
                $v->scene($scene);
            }
        }
        $v->message($message);
        if($batch || $this->batchValidate) {
            $v->batch(true);
        }
        return $v->failException(true)->check($data);
    }
    public function getCityByLongLat($lng, $lat)
    {
        $dis = ["uniacid" => 666];
        $config_model = new massage\model\Config();
        $config = $config_model->dataInfo($dis);
        $map_secret = !empty($config["map_secret"]) ? $config["map_secret"] : "bViFglag7C7G7QlZ1MglFyvh40yK1Tir";
        $URL = "https://apis.map.qq.com/ws/geocoder/v1/?location=" . $lat . "," . $lng . "&key=" . $map_secret;
        $data = longbingCurl($URL, []);
        $data = json_decode($data, true);
        $data = !empty($data["result"]["address_component"]["city"]) ? $data["result"]["address_component"]["city"] : "";
        return $data;
    }
    public function getCityByLongLatArr($lng, $lat, $uniacid)
    {
        $key = "getCityByLongLatArr" . round($lng, 4) . "-" . round($lat, 4);
        $value = getCache($key, $uniacid);
        if(!empty($value)) {
            return $value;
        }
        $dis = ["uniacid" => $uniacid];
        $config_model = new massage\model\Config();
        $config = $config_model->dataInfo($dis);
        $map_secret = !empty($config["map_secret"]) ? $config["map_secret"] : "bViFglag7C7G7QlZ1MglFyvh40yK1Tir";
        $URL = "https://apis.map.qq.com/ws/geocoder/v1/?location=" . $lat . "," . $lng . "&key=" . $map_secret;
        $data = longbingCurl($URL, []);
        $data = @json_decode($data, true);
        $arr = [];
        if(!empty($data["result"]["address_component"]["city"])) {
            $arr[] = $data["result"]["address_component"]["city"];
        }
        if(!empty($data["result"]["address_component"]["district"])) {
            $arr[] = $data["result"]["address_component"]["district"];
        }
        if(!empty($arr)) {
            setCache($key, $value, 86400, $uniacid);
        }
        return $arr;
    }
}

?>